package com.ebupt.migu.music.schedule;

import com.ebupt.migu.music.common.util.DateUtil;
import com.ebupt.migu.music.schedule.service.KafkaDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 与方舟系统交互相应定时任务
 */
@Slf4j
@Component
@EnableScheduling
public class KafkaDataTask {

    @Autowired
    private KafkaDataService kafkaDataService;

    @Value("${msg.pop_min_data_switch}")
    private boolean POP_MIN_DATA_SWITCH;

    /**
     * 获取变色龙实时数据
     * 每三分钟执行一次定时任务
     */
    @Scheduled(cron = "${cron.pop_min_data}")
    public void popupMinDataRealtime() {
        log.info("变色龙实时效果数据定时任务开始{},是否执行:{}", DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS),POP_MIN_DATA_SWITCH);
        try {
            if(POP_MIN_DATA_SWITCH) {
                //删除超过一周的变色龙实时数据
                kafkaDataService.deletePopupMinData();
                //存入标色龙实时数据进数据库中
                kafkaDataService.importPopupMinData();
            }
            log.info("变色龙实时效果数据定时任务结束{}", DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
        }catch (Exception e){
            log.error("{}变色龙实时效果数据定时任务异常",DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS),e);
        }
    }
}
